package com.example.zou0614.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.zou0614.pojo.Result;
import com.example.zou0614.pojo.StScore;
import com.example.zou0614.pojo.Student;
import com.example.zou0614.service.StScoreService;
import com.example.zou0614.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;

@CrossOrigin
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private StScoreService stScoreService;
    @Autowired
    private StudentService studentService;




    @RequestMapping("/getpage")
    public Result getPage(@RequestBody Student student){
        IPage<Student> studentPage = studentService.getStudentPage(student);

        Result result = new Result();
        result.setCount((int) studentPage.getTotal());
        result.setData(studentPage.getRecords());
        return result;
    }

    @RequestMapping("/insert")
    @Transactional
    public Result insert(@RequestBody Student insert) {

        StScore stScore = new StScore();
        stScore.setComputerperformance(insert.getComputerperformance());
        stScore.setInterviewresult(insert.getInterviewresult());
        if (insert.getComputerperformance() > 80 && insert.getInterviewresult()>80){
            stScore.setPass("及格");
        }else {
            stScore.setPass("不及格");
        }

        // 定义日期字符串格式

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 将字符串转换为LocalDate
        LocalDate localDate = LocalDate.parse(insert.getAsdf(), formatter);
            stScore.setAsdf(localDate.getMonthValue());

        boolean save1 = stScoreService.save(stScore);
        insert.setScoreid(stScore.getId());
        boolean save = studentService.save(insert);
        Result result = new Result();
        if (save && save1){
            result.setMsg("添加成功");
        }
        return result;
    }




    public static boolean time(String time){
        //将字符串转换为loacalDate对象
        LocalDate parse = LocalDate.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        //获取当前日期
        LocalDate now = LocalDate.now();
        //比较月份是否相同
        return parse.getYear() == now.getYear() && parse.getMonthValue() == parse.getMonthValue();
    }
}
